package com.qdsgjsfk.vision.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Toast;

import com.qdsgjsfk.vision.eventbus.Err;

import com.qdsgjsfk.vision.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import com.qdsgjsfk.vision.eventbus.SocketInput;
import com.qdsgjsfk.vision.socket.SocketServer;

import jz.joyoung.robot.util.LogUtil;
import util.PreferencesHelper;


/**
 * Created by HP on 2018/11/15.
 */

public class SocketService extends Service {

    //    private SocketClient client;
    private SocketServer server = new SocketServer(this);

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //适配安卓8.0
            NotificationChannel channel = new NotificationChannel("SocketService", "SocketService", NotificationManager.IMPORTANCE_MIN);
            channel.enableVibration(false);//去除振动

            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            assert manager != null;
            manager.createNotificationChannel(channel);
            Notification.Builder builder = new Notification.Builder(getApplicationContext(), "SocketService")
//                    .setContentTitle("正在后台运行")
                    .setSmallIcon(R.mipmap.logo_test);
            startForeground(1, builder.build());//id must not be 0,即禁止是0
        }
        EventBus.getDefault().register(this);
        server.beginListen();
        System.out.println("onCreate SocketService");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        server.close();
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    @Subscribe
    public void onMessageEvent(SocketInput socketInput) {
        //扫描二维码之后，udp发送数据，去连接视力表，
        if (!TextUtils.isEmpty(PreferencesHelper.getInstance().getString(this, "ipv4"))) {
//            LogUtil.e("SocketInput---" + socketInput.vision + "-----" + socketInput.port);
            server.sendMessage(socketInput.vision, PreferencesHelper.getInstance().getString(this, "ipv4"), socketInput.port);
        }  //            onErr( new Err( "视力表未连接" ) );

    }

    @Subscribe
    public void onErr(final Err err) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(() -> Toast.makeText(getApplicationContext(), err.err, Toast.LENGTH_SHORT).show());
    }
}
